package com.ihealthway.all.groupbuy.wechat;

import com.ihealthway.all.groupbuy.services.MPSettingService;
import com.ihealthway.all.groupbuy.services.UserInfoService;
import com.ihealthway.all.task.ExecutorThreadPool;
import com.ihealthway.all.groupbuy.vo.MPSetting;
import io.github.elkan1788.mpsdk4j.api.WechatAPIImpl;
import io.github.elkan1788.mpsdk4j.core.WechatDefHandler;
import io.github.elkan1788.mpsdk4j.vo.api.Follower;
import io.github.elkan1788.mpsdk4j.vo.event.BasicEvent;
import io.github.elkan1788.mpsdk4j.vo.event.MenuEvent;
import io.github.elkan1788.mpsdk4j.vo.message.*;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.util.Arrays;

/**
 * All Wechat event handler
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
public class GroupBuyingWechatHandler extends WechatDefHandler {

    private static final Log logger = Logs.get();

    @Inject
    private MPSettingService mpSetService;

    @Inject
    private UserInfoService userInfoService;

    @Inject
    private WechatAPIImpl wechatAPI;

    @Inject("java:$conf.get('server_domain')")
    private String serverDomain;

    @Override
    public BasicMsg text(TextMsg msg) {
        return super.text(msg);
    }

    @Override
    public BasicMsg eClick(MenuEvent event) {
        String key = event.getEventKey();
        switch (key) {
            case "personal-info":
                NewsMsg mClickMsg = new NewsMsg(event);
                Article article = new Article("点击百度搜索", "图文消息测试.",
                        "https://www.baidu.com/img/bd_logo1.png",
                        "https://www.baidu.com");
                mClickMsg.setArticles(Arrays.asList(article));
                return mClickMsg;
        }

        return super.eClick(event);
    }

    @Override
    public BasicMsg eSub(BasicEvent event) {

        MPSetting mpSet = this.mpSetService.get();
        TextMsg txtMsg = new TextMsg(event);

        ExecutorThreadPool.execute(() -> {
            Follower follower = wechatAPI.getFollower(event.getFromUserName(), "zh_CN");
            boolean flag = userInfoService.saveSubscribeUser(follower);
            logger.infof("Sync follower user info status: %b", flag);
            /*if (flag) {
                logger.infof("Add/Update openId[%s] subscribe success.", follower.getOpenid());
                NewsMsg newMsg = new NewsMsg(event);
                Article article = new Article("点击完善个人信息", "登记个人的信息,如公司，姓名等.",
                        "http://all.ihealthway.com/img/ind1_02.png",
                        String.format("http://%s/front/personal/register.do", serverDomain));
                newMsg.setArticles(Arrays.asList(article));
                wechatAPI.sendNewsMsg(newMsg);
            }*/

        });

        txtMsg.setContent(mpSet.getWelcome());
        return txtMsg;
    }

    @Override
    public void eUnSub(BasicEvent event) {

        ExecutorThreadPool.execute(() ->
                userInfoService.updateUnSubscribeUser(event.getFromUserName()));
    }
}
